package com.example.fitnessservice.module.dao.usermapper;

import com.example.fitnessservice.module.pojo.CourseDetails;
import com.example.fitnessservice.module.pojo.CourseDetailsCamp;
import com.example.fitnessservice.module.pojo.CourseOrderDetails;
import com.example.fitnessservice.module.pojo.Place;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/21 11:12
 */
@Mapper
public interface UserMapper {

    //查询用户的课程表
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "placeId", column = "place_id"),
            @Result(property = "courseCoachId", column = "course_coach_id"),
            @Result(property = "courseType", column = "course_type"),
            @Result(property = "courseStartTime", column = "course_start_time"),
            @Result(property = "courseEndTime", column = "course_end_time"),
            @Result(property = "courseStatus", column = "course_status"),
            @Result(property = "courseDescription", column = "course_description"),
            @Result(property = "courseMoney", column = "course_money"),
            @Result(property = "courseNum", column = "course_num"),
            @Result(property = "courseCoach", column = "course_coach_id", one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findClassByCid")),
            @Result(property = "place", column = "place_id", one = @One(select = "com.example.fitnessservice.module.dao.placemapper.PlaceMapper.findPlaceById")),
            @Result(property = "courseTypes", column = "course_type", one = @One(select = "com.example.fitnessservice.module.dao.courseMapper.CourseMapper.findCourseTypeById"))
    })
    @Select("select * from course_details cd, user_course uc where uc.user_id = #{userId} and uc.course_id = cd.id and uc.course_camp_if = 0 and cd.course_status = #{status} order by cd.course_end_time desc")
    public List<CourseDetails> findUserCourse1(@Param("userId") Integer userId, @Param("status") Integer status);

    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "longitude", column = "longitude"),
            @Result(property = "latitude", column = "latitude"),
            @Result(property = "startTime", column = "starttime"),
            @Result(property = "endTime", column = "endtime"),
            @Result(property = "telephone", column = "telephone"),
            @Result(property = "courseDetails", column = "id", many = @Many(select = "com.example.fitnessservice.module.dao.courseMapper.CourseMapper.findCourseByPlaceId")),
            @Result(property = "camps", column = "id", many = @Many(select = "com.example.fitnessservice.module.dao.courseMapper.CourseMapper.findCampByPlaceId")),
    })
    @Select("select * from place")
    public List<Place> findAllPlace();

    /**
     * 修改训练营的课程状态
     *
     * @return
     */
    @Update("update course_details_camp set course_status = 4 where NOW() < course_end_time and NOW() > course_start_time")
    public int updateCourseStatus2();

    @Update("update course_details_camp set course_status = 1 where NOW() > course_end_time")
    public int updateCourseStatus3();

    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "courseId", column = "course_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "placeId", column = "place_id"),
            @Result(property = "courseCoachId", column = "course_coach_id"),
            @Result(property = "courseStartTime", column = "course_start_time"),
            @Result(property = "courseEndTime", column = "course_end_time"),
            @Result(property = "courseStatus", column = "course_status"),
            @Result(property = "courseDescription", column = "course_description"),
            @Result(property = "courseMoney", column = "course_money"),
            @Result(property = "courseNum", column = "course_num"),
            @Result(property = "courseCoach", column = "course_coach_id", many = @Many(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findClassByCid")),
            @Result(property = "place", column = "place_id", one = @One(select = "com.example.fitnessservice.module.dao.placemapper.PlaceMapper.findPlaceById"))
    })
    @Select("select * from course_details_camp cdc, user_course uc where uc.user_id = #{userId} and uc.course_id = cdc.course_id and uc.course_camp_if = 1 and cdc.course_status = #{status} order by cdc.course_end_time desc")
    List<CourseDetailsCamp> findUserCamp1(@Param("userId") Integer userId, @Param("status") Integer status);

    //查询用户的课程表
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "placeId", column = "place_id"),
            @Result(property = "courseCoachId", column = "course_coach_id"),
            @Result(property = "courseType", column = "course_type"),
            @Result(property = "courseStartTime", column = "course_start_time"),
            @Result(property = "courseEndTime", column = "course_end_time"),
            @Result(property = "courseStatus", column = "course_status"),
            @Result(property = "courseDescription", column = "course_description"),
            @Result(property = "courseMoney", column = "course_money"),
            @Result(property = "courseNum", column = "course_num"),
            @Result(property = "courseCoach", column = "course_coach_id", one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findClassByCid")),
            @Result(property = "place", column = "place_id", one = @One(select = "com.example.fitnessservice.module.dao.placemapper.PlaceMapper.findPlaceById")),
            @Result(property = "courseTypes", column = "course_type", one = @One(select = "com.example.fitnessservice.module.dao.courseMapper.CourseMapper.findCourseTypeById"))
    })
    @Select("select * from course_details cd, user_course uc where uc.user_id = #{userId} and uc.course_id = cd.id and uc.course_camp_if = 0 order by cd.course_end_time desc")
    public List<CourseDetails> findUserCourse(Integer userId);

    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "courseId", column = "course_id"),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "placeId", column = "place_id"),
            @Result(property = "courseCoachId", column = "course_coach_id"),
            @Result(property = "courseStartTime", column = "course_start_time"),
            @Result(property = "courseEndTime", column = "course_end_time"),
            @Result(property = "courseStatus", column = "course_status"),
            @Result(property = "courseDescription", column = "course_description"),
            @Result(property = "courseMoney", column = "course_money"),
            @Result(property = "courseNum", column = "course_num"),
            @Result(property = "courseCoach", column = "course_coach_id", many = @Many(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findClassByCid")),
            @Result(property = "place", column = "place_id", one = @One(select = "com.example.fitnessservice.module.dao.placemapper.PlaceMapper.findPlaceById"))
    })
    @Select("select * from course_details_camp cdc, user_course uc where uc.user_id = #{userId} and uc.course_id = cdc.course_id and uc.course_camp_if = 1 order by cdc.course_end_time desc")
    List<CourseDetailsCamp> findUserCamp(Integer userId);

    @Select("select balance from user_info where user_id = #{userId}")
    public double findUserBalance(Integer userId);

    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderTime", column = "order_time"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "orderCoachId", column = "order_coach_id"),
            @Result(property = "orderUserId", column = "order_user_id"),
            @Result(property = "orderCourseId", column = "order_course_id"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "orderMoney", column = "order_money"),
            @Result(property = "courseDetails", column = "order_course_id", one = @One(select = "com.example.fitnessservice.module.dao.courseMapper.CourseMapper.findCourseDetailsById")),
            @Result(property = "coach", column = "order_coach_id", one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findClassByCid"))
    })
    @Select("select * from course_order_details where order_user_id = #{userId} and order_camp_if = 0")
    List<CourseOrderDetails> findAllCourseOrder(Integer userId);

    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderTime", column = "order_time"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "orderCoachId", column = "order_coach_id"),
            @Result(property = "orderUserId", column = "order_user_id"),
            @Result(property = "orderCourseId", column = "order_course_id"),
            @Result(property = "orderMoney", column = "order_money"),
            @Result(property = "courseDetails", column = "order_course_id", many = @Many(select = "com.example.fitnessservice.module.dao.courseMapper.CourseMapper.findCourseDetailsById")),
            @Result(property = "coach", column = "order_coach_id", one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findClassByCid"))
    })
    @Select("select * from course_order_details where order_user_id = #{userId} and order_camp_if = 0 and order_status = #{status}")
    List<CourseOrderDetails> findCourseOrder(@Param("userId") Integer userId, @Param("status") Integer status);

    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderTime", column = "order_time"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "orderCoachId", column = "order_coach_id"),
            @Result(property = "orderUserId", column = "order_user_id"),
            @Result(property = "orderCourseId", column = "order_course_id"),
            @Result(property = "orderStatus", column = "order_status"),
            @Result(property = "orderMoney", column = "order_money"),
            @Result(property = "courseDetailsCamp", column = "order_course_id", one = @One(select = "com.example.fitnessservice.module.dao.courseMapper.CourseMapper.findCampByCourseId")),
            @Result(property = "coach", column = "order_coach_id", one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findClassByCid"))
    })
    @Select("select * from course_order_details where order_user_id = #{userId} and order_camp_if = 1")
    List<CourseOrderDetails> findAllCampOrder(Integer userId);

    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "orderTime", column = "order_time"),
            @Result(property = "orderNo", column = "order_no"),
            @Result(property = "orderCoachId", column = "order_coach_id"),
            @Result(property = "orderUserId", column = "order_user_id"),
            @Result(property = "orderCourseId", column = "order_course_id"),
            @Result(property = "orderMoney", column = "order_money"),
            @Result(property = "courseDetailsCamp", column = "order_course_id", many = @Many(select = "com.example.fitnessservice.module.dao.courseMapper.CourseMapper.findCampByCourseId")),
            @Result(property = "coach", column = "order_coach_id", one = @One(select = "com.example.fitnessservice.module.dao.coachmapper.CoachMapper.findClassByCid"))
    })
    @Select("select * from course_order_details where order_user_id = #{userId} and order_camp_if = 1 and order_status = #{status}")
    List<CourseOrderDetails> findCampOrder(@Param("userId") Integer userId, @Param("status") Integer status);
}
